<template>
    <div class="achievement-list-page">
        <el-card shadow="never" class="mt-4">
            <div class="mt-3" v-loading="loading">
                <el-table :data="data">
                    <el-table-column type="index"
                    :index="indexMethod"
                    align="center"
                    label="序号"
                    width="50"/>
                    <el-table-column label="团队部门名称" prop="name"/>
                    <el-table-column label="团队部门描述" prop="describe"/>
                    <el-table-column label="企业" prop="company" v-if="!isCompanyAccount()"/>
                    <el-table-column label="操作">
                        <template slot-scope="{row}">
                            <el-button type="text" @click="onEditClick(row)">详情</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div v-if="totalElements > 0" class="footer">
                <pagination
                ref="pagination"
                :currentPage="form.page"
                :pageSize="form.size"
                :handleCurrentChange="onCurrentChange"
                :handlePageSizeChange="onPageSizeChange"
                :totalElements="totalElements"
                ></pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import Pagination from '../../../components/base/Pagination';
import IndexNumber from '../../../helpers/IndexNumber';

export default {
    components: {
        Pagination,
    },
    mixins: [require('../../../mixins/CacheQuery').default],
    data() {
        return {
            loading: false,
            data: [],
            form: {
                page: 1,
                size: 50,
                name: '',
                // 团队筛选
                type: 3,
            },
            totalElements: 0,
        };
    },
    filters: {
    },
    methods: {
        isCompanyAccount() {
            return this.$store.state.my.company_id > 0;
        },
        onSubmit() {
            this.form.page = 1;
            this.fetchData();
        },
        onResetFilters() {
            this.$refs.form.resetFields();
        },
        onCurrentChange(val) {
            this.form.page = val;
            this.fetchData();
        },
        onPageSizeChange(val) {
            this.form.size = val;
            this.form.page = 1;
            this.fetchData();
        },
        fetchData() {
            this.loading = true;
            API.get('role', {params: this.form}).then((res) => {
                this.data = res.content;
                this.totalElements = res.pagination.total;
            }).finally(() => {
                this.loading = false;
            });
        },
        onEditClick({id}) {
            if (this.isCompanyAccount()) {
                this.$router.push({
                    name: 'TeamReportManage.Detail',
                    params: { id: id },
                });
            } else {
                this.$router.push({
                    name: 'TeamStatisticsManage.Detail',
                    params: { id: id },
                });
            }
        },
        indexMethod(index) {
            return new IndexNumber()
                .getIndex(this.totalElements, this.form.page, this.form.size, index);
        },
        onExportClick({id}) {
            const url = 'statistics/export';
            const query = `/team/${id}`;
            window.location.href = `${process.env.VUE_APP_API_PREFIX}${url}${query}`;
        },
    },
    mounted() {
        this.fetchData();
    },
};
</script>

<style lang="scss">
.achievement-list-page {
    .filter {
        .input-width {
            max-width: 200px;
        }
    }
    .logo {
        width: 60px;
        height: 60px;
    }
    .footer {
        height: 44px;
        background: #f7f7f7;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        padding-left: 20px;
        padding-right: 20px;
    }
    .image-slot {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
        background: #f5f7fa;
        color: #909399;
    }
}
</style>
